Skip to content

Stop gradients through Qwen3 MoE expert-routing indices - #1787

Merged
zcbenz merged 3 commits into
ml-explore:mainfrom
nicejazzburger:fix/qwen3-moe-routing-index-gradients
Aug 28, 2026
Merged

zcbenz merged 3 commits into
ml-explore:mainfrom
nicejazzburger:fix/qwen3-moe-routing-index-gradients

Conversation

@nicejazzburger

Copy link
Copy Markdown
Contributor

What

Stops gradients through the integer top-k expert indices produced in Qwen3MoeSparseMoeBlock.

Why

The indices derive from differentiable router scores and are consumed by take_along_axis and SwitchGLU. Without an explicit barrier, LoRA backward propagation asks MLX for a VJP with respect to the indices and fails with:

ValueError: [gather_axis] Cannot calculate VJP with respect to indices.

Integer expert selections are non-differentiable. mx.stop_gradient(inds) preserves the forward computation while preventing the invalid gradient request. Router scores and model activations remain differentiable.

Tests

Adds tests/test_qwen3_moe_gradients.py, which constructs a small one-layer Qwen3 MoE model and verifies finite backward propagation:

  • without gradient checkpointing;
  • with MLX-LM gradient checkpointing.

Validation completed on current main:

black....................................................................Passed
isort....................................................................Passed
..................                                                       [100%]
18 passed

The change also completed one-step and twenty-step checkpointed LoRA smoke runs on Qwen3-30B-A3B-Instruct-2507-MLX-4bit, including adapter save and reload. The longer run used 1,736-token disposable inputs.

Fixes #1786

Related: ml-explore/mlx#3787, #1449, #1452

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the test to tests/test_tuner_trainer.py?

@zcbenz
zcbenz force-pushed the fix/qwen3-moe-routing-index-gradients branch 2 times, most recently from 766e5a7 to 603b9f6 Compare August 28, 2026 09:24
@zcbenz
zcbenz force-pushed the fix/qwen3-moe-routing-index-gradients branch from 603b9f6 to 230fa6d Compare August 28, 2026 09:26
@zcbenz
zcbenz force-pushed the fix/qwen3-moe-routing-index-gradients branch from 230fa6d to 2d613f0 Compare August 28, 2026 09:26
@zcbenz
zcbenz merged commit 1f9883c into ml-explore:main Aug 28, 2026
2 checks passed
adityak74 added a commit to adityak74/mlx-lm that referenced this pull request Aug 29, 2026
Per review: match the test placement used by ml-explore#1787 instead of a separate
test file.

Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
adityak74 added a commit to adityak74/mlx-lm that referenced this pull request Aug 29, 2026
…ybrid, lfm2_moe

LoRA fine-tuning (mlx_lm.lora --train) crashes on these three model families
with:

    ValueError: [gather_axis] Cannot calculate VJP with respect to indices.

The integer top-k expert indices derive from differentiable router logits and
flow into take_along_axis / the switch MLP, so backward propagation requests a
VJP w.r.t. the indices unless explicitly barriered. Most sibling MoE models
(olmoe, mixtral, qwen2_moe, phimoe, glm4_moe, ...) already wrap their routing
indices in mx.stop_gradient; these three were missing the guard.

Same fix as ml-explore#1787 (qwen3_moe), applied to the remaining affected families.
Adds a backward-pass regression test for all three.

Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
adityak74 added a commit to adityak74/mlx-lm that referenced this pull request Aug 29, 2026
Per review: match the test placement used by ml-explore#1787 instead of a separate
test file.

Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
hmason pushed a commit to HiddenDoor/mlx-lm that referenced this pull request Nov 11, 2026
)

* Fix Qwen3 MoE gradients through routing indices

* Test file has been moved as requested

* nit

---------

Co-authored-by: Jazzburger <264962389+nicejazzburger@users.noreply.github.com>
Co-authored-by: Cheng <git@zcbenz.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Qwen3 MoE LoRA backward fails through discrete expert-routing indices

2 participants